import cTitle from "components/title";
import date from "../date/member_date";
import {bindDrainageScene} from "../../../fun/modules/drainage_scene.js";
import { Toast } from 'vant';
export default {
  data() {
    return {
      sign_status: "", //签到状态
      sign_total: "", //累计签到天数
      continue_days: "", //连续签到天数
      cumulative: "", //累计奖励
      sign_log: "", //签到天数
      sign_name: "", //签到名字
      sharedata: {},
      signnowBol:false,
      nowDate:'',  //现在时间,
      username:'',//用户名
      uid:'',//用户ID
      avatar_image:'',//头像地址
      show_list:[],//签到奖励列表
    };
  },
  methods: {
    signnow() {
      let that = this;
      if(that.signnowBol){
        return false;
      }

      let isBindDrainageScene = bindDrainageScene({
        scene: 4,
        title: `${this.sign_name}`
      });
      if(!isBindDrainageScene) return;//爆客码绑定场景

      that.signnowBol = true;
      $http
        .get("plugin.sign.Frontend.Modules.Sign.Controllers.sign.sign", {}, " ")
        .then(res => {
          if (res.result === 1) {
            // var reslink = res.data.success_url;
            that.$dialog.alert({ message: res.msg})
              .then(() => {
                if(res.data.success_url && location.href != res.data.success_url){
                  // console.log(res.data.success_url);
                  location.href = res.data.success_url;
                }else{
                  that.getSign();
                }

              }).catch(() => {});

          } else {
            Toast(res.msg);
            console.log(res);
          }
          // that.signnowBol = false;
        });
    },
    getData() {
      let that = this;
      $http
        .get("plugin.sign.Frontend.Controllers.share.index", {}, " ")
        .then(function(response) {
          if (response.result === 1) {
            // console.log(response);
            that.sharedata = response.data.share;
            that.fun.wxShare(
              "",
              {},
              {
                title: that.sharedata.share_title ? that.sharedata.share_title : '',
                imgUrl: that.sharedata.share_icon ? that.sharedata.share_icon : '',
                description: that.sharedata.share_desc ? that.sharedata.share_desc : ''
              }
            );
          }
        });
    },
    getSign() {
      $http
        .get(
          "plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index",
          {},
          "loading"
        )
        .then(res => {
          // console.log(res.data);
          this.sign_status = res.data.sign_status;
          this.sign_total = res.data.sign_total;
          this.continue_days = res.data.continue_days;
          this.cumulative = window.localStorage.integral + res.data.cumulative.substr(2);
          this.sign_log = res.data.sign_log;
          this.sign_name = res.data.sign_name;
          this.username=res.data.member.username;
          this.uid=res.data.member.uid;
          this.avatar_image = res.data.member.avatar_image;
          this.fun.setWXTitle(this.sign_name || '签到');
          this.isShow(res.data.show_list);
          if(res.basic_info){
            localStorage.setItem('basic_color',res.basic_info.theme_color);
            let root = document.querySelector(":root");
            root.style.setProperty("--themeBaseColor", res.basic_info.theme_color) ;
          }
        });
    },
    getDateTime (type) {
      var date = new Date();
      var hengGang = "-";
      var maoHao = ":";
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var curDate = date.getDate();
      var curHours = date.getHours();
      var curMinutes = date.getMinutes();
      var curSeconds = date.getSeconds();

      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (curDate >= 0 && curDate <= 9) {
        curDate = "0" + curDate;
      }
      if (curHours >= 0 && curHours <= 9) {
        curHours = "0" + curHours;
      }
      if (curMinutes >= 0 && curMinutes <= 9) {
        curMinutes = "0" + curMinutes;
      }
      if (curSeconds >= 0 && curSeconds <= 9) {
        curSeconds = "0" + curSeconds;
      }
      var currentdate = "";
      if (type == "year") {
        currentdate = year;
        return currentdate;
      } else if (type == "month") {
        currentdate = year + hengGang + month;
        return currentdate;
      } else {
        currentdate = year + hengGang + month + hengGang + curDate + " " + curHours + ' ' + maoHao + ' ' + curMinutes +  ' ' + maoHao + ' ' + curSeconds;
        return currentdate;
      }
    },
    isShow(show_list){
      let arr = show_list.filter(item => {
        {
          return Number(item.total);
        }
      });
      this.show_list = arr;
    }
  },
  mounted(){
    this.signnowBol = false;
    this.nowDate = new Date();
  },
  activated() {
    this.signnowBol = false;
    this.getSign();
    this.getData();
  },
  computed:{
    signTotal(){
      if(this.sign_total =='暂无'){
        return 0;
      }else{
        return this.sign_total.substring(0,this.sign_total.length-1);
      }
    },
    continueDays(){
      if(this.continue_days == '暂无'){
        return 0;
      }
      else
      {return this.continue_days.substring(0,this.continue_days.length-1);}
    },
  },
  components: { cTitle, date },
};
